//
//  UIFunc.m
//  SendGzipData
//
//  Created by URANUS on 15/4/14.
//  Copyright (c) 2015年 URANUS. All rights reserved.
//

#import "UIFunc.h"

@implementation UIFunc


#pragma mark 通过拿到Config的设置换算成屏幕成员的坐标和大小
+ (CGRect) getUIViewFrame:(NSString *)posInfo withSize:(CGSize)size{
    CommPos commPos = [CommFunc getPos:posInfo];
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGFloat x = width * commPos.x;
    CGFloat y = height * commPos.y;
    CGFloat w = width * commPos.w;
    CGFloat h = height* commPos.h;
    CGRect frame = CGRectMake(x, y, w, h);
    
    return frame;
}

#pragma mark 通过配置文件获取成员的W 和H
+ (CGSize) getUIViewSize:(NSString *)posInfo withSize:(CGSize)size{
    CommPos commPos = [CommFunc getPos:posInfo];
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGFloat w = width * commPos.w;
    CGFloat h = height* commPos.h;
    CGSize viewSize = CGSizeMake(w, h);
    
    return viewSize;
}

#pragma mark 通过配置文件获取成员的X和Y
+ (CGPoint) getUIViewOrigin:(NSString *)posInfo withSize:(CGSize)size{
    CommPos commPos = [CommFunc getPos:posInfo];
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGFloat x = width * commPos.x;
    CGFloat y = height * commPos.y;
    CGPoint viewOrigin = CGPointMake(x, y);
    
    return viewOrigin;
}

#pragma mark 设置一个view的frame
+ (void) setUIViewFrame:(UIView *)view withPosInfo:(NSString *)posInfo withSize:(CGSize)size{
    CGRect frame =[self getUIViewFrame:posInfo withSize:size];
    view.frame = frame;
}

#pragma mark 设置一个view的size
+ (void) setUIViewSize:(UIView *)view withPosInfo:(NSString *)posInfo withSize:(CGSize)size{
    CGSize viewSize = [self getUIViewSize:posInfo withSize:size];
    view.bounds = CGRectMake(0, 0, viewSize.width, viewSize.height);
}

#pragma mark 设置一个物体的X，Y坐标
+ (void) setUIViewOrigin:(UIView *)view withPosInfo:(NSString *)posInfo withSize:(CGSize)size{
    CGPoint origin = [self getUIViewOrigin:posInfo withSize:size];
    view.frame = CGRectMake(origin.x, origin.y, view.bounds.size.width, view.bounds.size.height);
}

#pragma mark 设置一个button的前景和背景色RGB
+ (void) setUIButton:(UIButton *)button withRGB:(NSString *)rgbString{
    NSArray *rgbArray = [rgbString componentsSeparatedByString:@"|"];
    CommRGB rgb1 = [CommFunc getRGB:[rgbArray objectAtIndex:0]];
    button.backgroundColor = [UIColor colorWithRed:rgb1.r green:rgb1.g blue:rgb1.b alpha:1.0];
    CommRGB rgb2 = [CommFunc getRGB:[rgbArray objectAtIndex:1]];
    [button setTitleColor:[UIColor colorWithRed:rgb2.r green:rgb2.g blue:rgb2.b alpha:1.0] forState:UIControlStateNormal];
}

#pragma mark 设置labal的RGB
+ (void) setUILabelRGB:(UILabel *)label withRGB:(NSString *)rgbString{
    NSArray *rgbArray = [rgbString componentsSeparatedByString:@"|"];
    CommRGB rgb1 = [CommFunc getRGB:[rgbArray objectAtIndex:0]];
    label.backgroundColor = [UIColor colorWithRed:rgb1.r green:rgb1.g blue:rgb1.b alpha:1.0];
    CommRGB rgb2 = [CommFunc getRGB:[rgbArray objectAtIndex:1]];
    [label setTextColor:[UIColor colorWithRed:rgb2.r green:rgb2.g blue:rgb2.b alpha:1.0]];
}

#pragma mark 设置textfield的RGB
+ (void) setUITextField:(UITextField *)textField withRGB:(NSString *)rgbString{
    NSArray *rgbArray = [rgbString componentsSeparatedByString:@"|"];
    CommRGB rgb1 = [CommFunc getRGB:[rgbArray objectAtIndex:0]];
    textField.backgroundColor = [UIColor colorWithRed:rgb1.r green:rgb1.g blue:rgb1.b alpha:1.0];
    CommRGB rgb2 = [CommFunc getRGB:[rgbArray objectAtIndex:1]];
    [textField setTextColor:[UIColor colorWithRed:rgb2.r green:rgb2.g blue:rgb2.b alpha:1.0]];
}

#pragma mark 设置uiView frame size origin(坐标)
+ (void) setUIView:(UIView *)view dict:(NSDictionary *)dict propertyName:(NSString*)propertyName withPropertyType:(NSInteger) propertyType withSize:(CGSize)size{
    NSString *propertyValue = [dict objectForKey:propertyName];
    
    if (propertyType == FRAME) {
        [self setUIViewFrame:view withPosInfo:propertyValue withSize:size];
    } else if (propertyType == SIZE) {
        [self setUIViewSize:view withPosInfo:propertyValue withSize:size];
    } else if (propertyType == ORIGIN){
        [self setUIViewOrigin:view withPosInfo:propertyValue withSize:size];
    }
}

+ (void) setUIButton:(UIButton *)button dict:(NSDictionary *)dict rgbName:(NSString *)rgbName{
    NSString *rgbValue = [dict objectForKey:rgbName];
    [self setUIButton:button withRGB:rgbValue];
}

+ (void) setUILabel:(UILabel *)label dict:(NSDictionary *)dict rgbName:(NSString *)rgbName{
    NSString *rgbValue = [dict objectForKey:rgbName];
    [self setUILabelRGB:label withRGB:rgbValue];
}

+ (void) setUIButtonTitleSize:(UIButton *)button dict:(NSDictionary *)dict propertyName:(NSString *) propertyName{
    NSString *propertyValue = [dict objectForKey:propertyName];
    NSInteger fontSize = [propertyValue integerValue];
    button.titleLabel.font = [UIFont systemFontOfSize: fontSize];
}

+ (void) setUILabelFontSize:(UILabel *)label dict:(NSDictionary *)dict propertyName:(NSString *) propertyName{
    NSString *propertyValue = [dict objectForKey:propertyName];
    NSInteger fontSize = [propertyValue integerValue];
    label.font = [UIFont systemFontOfSize: fontSize];
}

+ (void) setUIButtonProperty:(UIButton *)button dict:(NSDictionary *)dict frameName:(NSString *)frameName rgbName:(NSString *)rgbName fontSize:(NSString *)fontSizeName andSize:(CGSize)size{
    [self setUIView:button dict:dict propertyName:frameName withPropertyType:FRAME withSize:size];
    [self setUIButton:button dict:dict rgbName:rgbName];
    [self setUIButtonTitleSize:button dict:dict propertyName:fontSizeName];
}

+ (void) setUILabelProperty:(UILabel *)label dict:(NSDictionary *)dict frameName:(NSString *)frameName rgbName:(NSString *)rgbName fontSize:(NSString *)fontSizeName andSize:(CGSize)size{
    [self setUIView:label dict:dict propertyName:frameName withPropertyType:FRAME withSize:size];
    [self setUILabel:label dict:dict rgbName:rgbName];
    [self setUILabelFontSize:label dict:dict propertyName:fontSizeName];
}

#pragma mark 在view上显示菊花信息
+ (void) addUIViewIndicatorView:(UIView *) view{
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    CGRect viewFrame = view.frame;
    //添加一个灰色的面板用于屏蔽当前操作
    UIView *shadowView = [[UIView alloc] initWithFrame:viewFrame];
    shadowView.backgroundColor = [UIColor blackColor];
    shadowView.alpha = 0.2;
    shadowView.tag = INDICATORSHADOWTAG;
    [view addSubview:shadowView];
    
    //添加一菊花
    indicatorView.tag = INDICATORTAG;
    indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [indicatorView setCenter:CGPointMake(CGRectGetMidX(viewFrame), CGRectGetMidY(viewFrame))];
    [view addSubview:indicatorView];
    [indicatorView startAnimating];
}

#pragma mark 清除在屏幕上的菊花
+ (void) removeUIViewIndicatorView:(UIView *) view{
    NSArray *subViews = [view subviews];
    
    for (UIView *subView in subViews) {
        if (subView.tag == INDICATORTAG) {
            UIActivityIndicatorView *indicatorView = (UIActivityIndicatorView *) subView;
            [indicatorView stopAnimating];
            [indicatorView removeFromSuperview];
        } else if (subView.tag == INDICATORSHADOWTAG) {
            [subView removeFromSuperview];
        }
    }
}

#pragma mark 设置button的字体大小按照Button比例
+(void)setupButton:(UIButton *) button FontSize:(CGFloat) textBili{
    CGFloat button_Y = CGRectGetHeight(button.frame);
    if (textBili <= 0) {
        button.titleLabel.font = [UIFont systemFontOfSize:(button_Y * 4 /5)];
    }else{
        button.titleLabel.font = [UIFont systemFontOfSize:(button_Y * textBili)];
    }
}

#pragma mark 通过label的高度设置text的大小, 如果为负数则设置为高度的4/5
+ (void) setLabel:(UILabel *)label titleSize:(CGFloat)size{
    CGFloat height = CGRectGetHeight(label.frame);
    if (size <= 0) {
        label.font = [UIFont systemFontOfSize:(height * 4 / 5)];
    }else{
        label.font = [UIFont systemFontOfSize:(height * size)];
    }
}

#pragma mark 通过label的高度设置textField的大小, 如果为负数则设置为高度的4/5
+ (void) setTextField:(UITextField *)textField textSize:(float)size{
    CGFloat height = CGRectGetHeight(textField.frame);
    if (size <= 0) {
        textField.font = [UIFont systemFontOfSize:(height * 4 / 5)];
    }else{
        textField.font = [UIFont systemFontOfSize:(height * size)];
    }
}

//struct ConfigRGB {
//    CGFloat r;
//    CGFloat g;
//    CGFloat b;
//};
//typedef struct ConfigRGB ConfigRGB;

#pragma mark 在Config.plist中拿RGB值生成CGColorRef数据
+(CGColorRef) getCGColorForConfig:(NSString *)dictKey ConfigName:(NSString *)dictName Alpha:(int) alpha{
    NSDictionary *pageDict = [CommFunc getPageDirctionary:dictName];
    NSString *dictColor = [pageDict objectForKey:dictKey];
    CommRGB theRGB = [CommFunc getRGB:dictColor];
    CGFloat r = (CGFloat) theRGB.r;
    CGFloat g = (CGFloat) theRGB.g;
    CGFloat b = (CGFloat) theRGB.b;
    CGFloat a = (CGFloat) alpha;
    CGFloat components[4] = {r,g,b,a};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = (CGColorRef)CGColorCreate(colorSpace, components);
    CGColorSpaceRelease(colorSpace);
    return color;
}



@end
